package com.sduept.nwld.dataserver.controller.fault;

import com.sduept.bigdata.fault.entity.ComponentDictionary;
import com.sduept.bigdata.fault.manager.ComponentDictionaryManager;
import com.sduept.bigdata.web.jsf.controller.AbstractController;
import com.sduept.core.dao.SuperDAO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.inject.Named;
import javax.faces.view.ViewScoped;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 故障档案动作元件维护字典控制
 * @author RY
 *
 */
@Named
@ViewScoped
public class ComponentDictionaryController extends AbstractController{
	private static final long serialVersionUID = 5559613618390097799L;

	@Resource
	private DataSource serverDatasource;
	@Autowired
	private SuperDAO dao;
	@Autowired
	private ComponentDictionaryManager manager;
	
	private List<ComponentDictionary> compontentDics = new ArrayList<ComponentDictionary>();
	private ComponentDictionary current;
	private String protectModel;
	private List<String>funClassiFications = new ArrayList<String>();
	
	@PostConstruct
	public void init(){
		compontentDics = manager.findAllDict();
		initFunClassiFications();
		
	}
	
	/**
	 * 导入故障档案的数据到字典表
	 * 初始化数据时调用
	 */
	private void importDataFromFaultReport(){
		Connection conn = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		try {
			conn = serverDatasource.getConnection();
			pst = conn.prepareStatement("select d.protectModel,d.actionComponent,d.funClassification from t_fault_report_component_detail d where d.protectModel is not null");
			rs =  pst.executeQuery();
			while(rs.next()){
				String proModel = rs.getString("protectModel");
				String component = rs.getString("actionComponent");
				String fication = rs.getString("funClassification");
				ComponentDictionary dict = new ComponentDictionary();
				dict.setProtectModel(proModel);
				dict.setFunClassification(fication);
				dict.setActionComponent(component);
				manager.saveComponentDict(dict);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			try {
				pst.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
	}
	
	private void initFunClassiFications(){
		funClassiFications.add("重合闸");
		funClassiFications.add("主保护");
		funClassiFications.add("后备保护");
		funClassiFications.add("断路器保护");
	}
	
	public void search(){
		if (StringUtils.isNotEmpty(protectModel)) {
			compontentDics = manager.findComponents(protectModel, false);
		}else{
			compontentDics = manager.findAllDict();
		}
	}
	
	public void reset(){
		protectModel = "";
		compontentDics = manager.findAllDict();
	}
	
	public void preCreate(){
		current = new ComponentDictionary();
	}
	
	public void save(){
		if (StringUtils.isNotEmpty(current.getId())) {
			current = manager.saveComponentDict(current);
			if (null != current) {
				addSuccessMessage("保存成功！");
			}else{
				addErrorMessage("参数为空，或字典已存在！");
			}
		}else{
			current = manager.saveComponentDict(current);
			if (null != current) {
				compontentDics.add(current);
				addSuccessMessage("保存成功！");
			}else{
				addErrorMessage("参数为空，或字典已存在！");
			}
		}
	}
	
	public void delete(){
		manager.delete(current);
		compontentDics.remove(current);
		current = null;
	}
	
	public List<String> complateStation(String query){
		return funClassiFications;
	}

	public List<ComponentDictionary> getCompontentDics() {
		return compontentDics;
	}

	public void setCompontentDics(List<ComponentDictionary> compontentDics) {
		this.compontentDics = compontentDics;
	}

	public ComponentDictionary getCurrent() {
		return current;
	}

	public void setCurrent(ComponentDictionary current) {
		this.current = current;
	}

	public String getProtectModel() {
		return protectModel;
	}

	public void setProtectModel(String protectModel) {
		this.protectModel = protectModel;
	}
	
}
